/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shellCommands;

import Sessions.EcomAdminRemote;
import Sessions.EcomCustomersRemote;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author SOGOBA
 */
public class ShellAdmin extends ShellSuperClass{
   boolean stop=false;
   EcomAdminRemote ecomAdminBean;
      public ShellAdmin(){
        super();

        try {
            InitialContext sc = new InitialContext();
           //    StringBuffer res = new StringBuffer();
            ecomAdminBean = (EcomAdminRemote) sc.lookup("java:global/StoreEcom/StoreEcom-ejb/EcomAdmin");
        } catch (NamingException ex) {
            Logger.getLogger(ShellCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
      
     @SuppressWarnings("CallToThreadDumpStack")
    public final void run() {
         String banner;
            banner = (String) super.getContext().getVar("BANNER");
            if (banner != null) {
                System.out.println(banner);
            }
             br =new Scanner(System.in);
       while (stop==false) {
            try {
                String prompt;
                prompt = (String) super.getContext().getVar("PROMPT");
                if (prompt == null) {
                    prompt = "->";
                }
                System.out.println("Veillez saisir une commande");
                System.out.print(prompt);
                //     System.out.println("kjkjhkjhkjhkjhkhkjhk");
                //      line = line.trim();
                line = br.nextLine();
                executeCommand(line);
                //       }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    }
    }
    
     public synchronized void executeCommand(String commandLine) throws Exception {
        commandLine = commandLine.trim();
         String commandName;
        if (commandLine.indexOf(' ') >= 0){
        commandName =commandLine.substring(0, commandLine.indexOf(' '));
        }
       else{
            commandName=commandLine;
       }
        InterShell command = getCommand(commandName);
        if (command==null){
            System.out.println("Command is not avalaible");
        }else {
             
          if (command.getName()==InterShell.Commands.cart || command.getName()==InterShell.Commands.connect) {
                System.out.println("Privelege problem to run this command");
        }  
          if (command.getName()==InterShell.Commands.customer || command.getName()==InterShell.Commands.category || command.getName()==InterShell.Commands.store || command.getName()==InterShell.Commands.language || command.getName()==InterShell.Commands.output ||command.getName()==InterShell.Commands.product || command.getName()==InterShell.Commands.currency || command.getName()==InterShell.Commands.help || command.getName()==InterShell.Commands.quit) {
                    command.execute(commandLine, ecomAdminBean, null);
          }              
          }
            
    } 
           
}
